package com.collect.tanzania.zhiScene.zhiTelemarketing.zhiPerformance;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.collect.tanzania.R;
import com.collect.tanzania.zhiAdater.zhiLevelDetail.ZHILevelDetailTelemarketing;
import com.collect.tanzania.zhiBase.BaseFragment;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRTelemarketingPerformanceEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRTelemarketingPerformanceListEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.tanzania.zhiHelper.ZHIMainProcessHelper;
import com.collect.tanzania.zhiScene.zhiTelemarketing.zhiPerformance.zhiSort.ZHITelemarketingCompareByAssignNumASC;
import com.collect.tanzania.zhiScene.zhiTelemarketing.zhiPerformance.zhiSort.ZHITelemarketingCompareByAssignNumDESC;
import com.collect.tanzania.zhiScene.zhiTelemarketing.zhiPerformance.zhiSort.ZHITelemarketingCompareByBonusASC;
import com.collect.tanzania.zhiScene.zhiTelemarketing.zhiPerformance.zhiSort.ZHITelemarketingCompareByBonusDESC;
import com.collect.tanzania.zhiScene.zhiTelemarketing.zhiPerformance.zhiSort.ZHITelemarketingCompareByDateASC;
import com.collect.tanzania.zhiScene.zhiTelemarketing.zhiPerformance.zhiSort.ZHITelemarketingCompareByDateDESC;
import com.collect.tanzania.zhiScene.zhiTelemarketing.zhiPerformance.zhiSort.ZHITelemarketingCompareByNumASC;
import com.collect.tanzania.zhiScene.zhiTelemarketing.zhiPerformance.zhiSort.ZHITelemarketingCompareByNumDESC;
import com.collect.tanzania.zhiUtils.ToastUtil;
import com.collect.tanzania.zhiUtils.ZHICalendarUtil;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIPerformanceTelemarketingFragment extends BaseFragment<ZHIPerformanceTelemarketingPresenter> implements ZHIPerformanceTelemarketingContract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.tv_cumulative_bonus_date)
    TextView tv_cumulative_bonus_date;
    @BindView(R.id.iv_cumulative_bonus_date)
    ImageView iv_cumulative_bonus_date;

    @BindView(R.id.tv_total_assign_num)
    TextView tv_total_assign_num;
    @BindView(R.id.tv_total_bonus)
    TextView tv_total_bonus;
    @BindView(R.id.tv_total_task)
    TextView tv_total_task;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    @BindView(R.id.iv_sort)
    ImageView iv_sort;

    @BindView(R.id.iv_note)
    ImageView iv_note;
    @BindView(R.id.iv_filter)
    ImageView iv_filter;

    @BindView(R.id.ll_assign_date_bg)
    LinearLayout ll_assign_date_bg;

    @BindView(R.id.ll_assign_num_bg)
    LinearLayout ll_assign_num_bg;

    @BindView(R.id.ll_apply_num_bg)
    LinearLayout ll_apply_num_bg;

    @BindView(R.id.ll_bonus_bg)
    LinearLayout ll_bonus_bg;

    private boolean mIsSortAssignDateDes = true;
    private boolean mIsSortAssignNumDes = false;
    private boolean mIsSortApplyNumDes = false;
    private boolean mIsSortBonusDes = false;

    private ArrayList<ZHIRTelemarketingPerformanceEntity> mTelemarketingPerformanceEntityArrayList;

    private final ZHILevelDetailTelemarketing mZHILevelDetailTelemarketing = new ZHILevelDetailTelemarketing();

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_perfermance_telemarketing;
    }

    @Override
    protected void initView(View view) {
        try {
            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            tv_page_title.setOnClickListener(v -> {
            });

            iv_note.setOnClickListener(v -> ToastUtil.s("Features will be opened soon."));

            iv_sort.setOnClickListener(v -> {

            });

            ll_assign_date_bg.setOnClickListener(v -> {
                try {
                    if (mTelemarketingPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortAssignDateDes) {
                        mIsSortAssignDateDes = false;
                        Collections.sort(mTelemarketingPerformanceEntityArrayList, new ZHITelemarketingCompareByDateASC());
                    } else {
                        mIsSortAssignDateDes = true;
                        Collections.sort(mTelemarketingPerformanceEntityArrayList, new ZHITelemarketingCompareByDateDESC());
                    }

                    mZHILevelDetailTelemarketing.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_assign_num_bg.setOnClickListener(v -> {
                try {
                    if (mTelemarketingPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortAssignNumDes) {
                        mIsSortAssignNumDes = false;
                        Collections.sort(mTelemarketingPerformanceEntityArrayList, new ZHITelemarketingCompareByAssignNumASC());
                    } else {
                        mIsSortAssignNumDes = true;
                        Collections.sort(mTelemarketingPerformanceEntityArrayList, new ZHITelemarketingCompareByAssignNumDESC());
                    }

                    mZHILevelDetailTelemarketing.notifyDataSetChanged();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_apply_num_bg.setOnClickListener(v -> {
                try {
                    if (mTelemarketingPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortApplyNumDes) {
                        mIsSortApplyNumDes = false;
                        Collections.sort(mTelemarketingPerformanceEntityArrayList, new ZHITelemarketingCompareByNumASC());
                    } else {
                        mIsSortApplyNumDes = true;
                        Collections.sort(mTelemarketingPerformanceEntityArrayList, new ZHITelemarketingCompareByNumDESC());
                    }

                    mZHILevelDetailTelemarketing.notifyDataSetChanged();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_bonus_bg.setOnClickListener(v -> {
                try {
                    if (mTelemarketingPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortBonusDes) {
                        mIsSortBonusDes = false;
                        Collections.sort(mTelemarketingPerformanceEntityArrayList, new ZHITelemarketingCompareByBonusASC());
                    } else {
                        mIsSortBonusDes = true;
                        Collections.sort(mTelemarketingPerformanceEntityArrayList, new ZHITelemarketingCompareByBonusDESC());
                    }

                    mZHILevelDetailTelemarketing.notifyDataSetChanged();

                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            iv_filter.setOnClickListener(v -> {

            });

            iv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toPerformanceTelemarketingHistory());
            tv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toPerformanceTelemarketingHistory());

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetTelemarketingPerformance(ZHIRTelemarketingPerformanceListEntity zhirTelemarketingPerformanceListEntity, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            ///////////////////////
            tv_total_assign_num.setText(zhirTelemarketingPerformanceListEntity.getBonusOrderNum());
            tv_total_bonus.setText(zhirTelemarketingPerformanceListEntity.getMonthBonus());
            tv_total_task.setText(zhirTelemarketingPerformanceListEntity.getMonthlyTask());

            ///////////////////////
            ArrayList<ZHIRTelemarketingPerformanceEntity> zhirTelemarketingPerformanceEntityArrayList = zhirTelemarketingPerformanceListEntity.getDataList();
            Collections.sort(zhirTelemarketingPerformanceEntityArrayList, new ZHITelemarketingCompareByDateDESC());
            mTelemarketingPerformanceEntityArrayList = zhirTelemarketingPerformanceEntityArrayList;
            mZHILevelDetailTelemarketing.setNewData(zhirTelemarketingPerformanceEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(() -> initData(false));

        mZHILevelDetailTelemarketing.setEmptyView(getRefreshEmptyView());

        rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_detail_list.setAdapter(mZHILevelDetailTelemarketing);
    }

    public void initData(boolean showDialog) {
        if (mPresenter != null) {
            ZHISPerfermanceEntity zhisPerfermanceEntity = getCaseSearchParams();
            mPresenter.getTelemarketingPerformance(getActivity(), zhisPerfermanceEntity, showDialog);
        }

    }

    public void refreshData() {
        initData(true);
    }

    private ZHISPerfermanceEntity getCaseSearchParams() {
        //return mZHIRootGroupFragmentInterface.onGetPerfermanceFilterParams();

        String gStartDate = ZHICalendarUtil.getCurMonthFirstDayFormatString();
        String gEndDate = ZHICalendarUtil.getCurMonthCurDayFormatString();

        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        zhisPerfermanceEntity.setLeven(ZHIMainProcessHelper.getInstance().getLoginEntity().getDefaultLevenApiFormat());
        zhisPerfermanceEntity.setStartDateForDisplay(gStartDate);
        zhisPerfermanceEntity.setEndDateForDisplay(gEndDate);
        zhisPerfermanceEntity.setStartDate(gStartDate);
        zhisPerfermanceEntity.setEndDate(gEndDate);
        zhisPerfermanceEntity.setLeaderUserId(ZHIMainProcessHelper.getInstance().getLoginEntity().getUserId());
        return zhisPerfermanceEntity;
    }

    @SuppressLint("InflateParams")
    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        return emptyView;
    }
}
